<template>
  <div>
    <el-dialog
      :title="formData.id ? '编辑事件' : '新建事件'"
      v-model="isAdd"
      width="500px"
      :before-close="closeListAdd"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="formData"
        label-width="100px"
      >
        <el-form-item label="事件CODE" prop="propertyKey">
          <el-input
            type="text"
            placeholder="请输入数字、字母、下划线"
            clearable
            v-model="formData.propertyKey"
            @input="setValue('propertyKey')"
            maxlength="10"
            :show-word-limit="true"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="事件名称" prop="name">
          <el-input
            type="text"
            placeholder="请输入事件名称"
            v-model.trim="formData.name"
            clearable
            maxlength="40"
            show-word-limit
          >
          </el-input>
        </el-form-item>

        <el-form-item label="类型" prop="propertyType">
          <el-select v-model="formData.propertyType" placeholder="请选择类型">
            <el-option
              v-for="item in propertyType"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="事件分类" prop="typeId">
          <el-select v-model="formData.typeId" placeholder="请选择事件分类">
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="设置快捷入口" prop="showInCanvas">
          <el-switch
            v-model="formData.showInCanvas"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item>
        <!-- <el-form-item
          v-if="formData.showInCanvas == 1"
          label="选择组件icon"
          prop="remark"
        >
          <el-tag
            v-for="(item, idx) in iconList"
            :key="idx"
            :class="{ 'tag-active': formData.icon == item }"
            @click="formData.icon = item"
          >
            <i :class="item"></i>
          </el-tag>
        </el-form-item> -->
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeListAdd()">取 消</el-button>
          <el-button type="primary" @click="formValidate">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import EventApi from "@/api/eventApi";
export default {
  props: {
    isAdd: {
      require: false,
      type: Boolean,
      default: () => {
        return false;
      },
    },
    typeList: {
      require: false,
      type: Array,
      default: () => {
        return [];
      },
    },
    addData: {
      require: false,
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  watch: {
    isAdd(val) {
      this.formData = this.addData;
    },
  },
  data() {
    return {
      index: "",
      formData: {
        name: "",
        icon: "",
        propertyKey: "",
        showInCanvas: 0,
        typeId: "",
        propertyType: "",
      },
      propertyType: [
        { id: 0, name: "数字" },
        { id: 1, name: "字符串" },
        { id: 2, name: "时间" },
        { id: 3, name: "日期" },
      ],
      iconList: [
        "el-icon-tickets",
        "el-icon-document-checked",
        "el-icon-attract",
        "el-icon-data-analysis",
      ],
      rules: {
        propertyKey: [
          { required: true, message: "请输入事件CODE", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入事件名称", trigger: "blur" }],
        propertyType: [
          { required: true, message: "请选择事件分类", trigger: "blur" },
        ],
        typeId: [{ required: true, message: "请选择类型", trigger: "blur" }],
      },
    };
  },
  methods: {
    formValidate() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.saveOrUpdateEvent();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async saveOrUpdateEvent() {
      let rs = await EventApi.saveOrUpdateEvent(this.formData);
      if (rs.code == 200) {
        ElMessage.success(
          this.addData.id ? "修改事件成功" : "添加事件成功"
        );
        this.$emit("addList");
        this.closeListAdd();
      }
    },
    setValue(propertyKey) {
      this.formData[propertyKey] = this.formData[propertyKey].replace(
        /[^\w_]/g,
        ""
      );
    },
    closeListAdd() {
      this.$emit("closeListAdd");
      this.$refs.formData.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-tag {
  margin-right: 10px;
  cursor: pointer;
}
.tag-active {
  color: red;
}
</style>